import { login } from "@/http/user.js"
export let user = {
    namespaced: true,
    state: {
        token: localStorage.getItem("houtaitoken"),
        get username() {
            return localStorage.getItem("houtaiusername");
        },
        set username(v) {
            if (v) {
                localStorage.setItem("houtaiusername", v)
            } else {
                localStorage.removeItem("houtaiusername")
            }


        },
    },
    actions: {
        // 请求登录 接口
        async loginFn({ commit }, obj) {
            // console.log("请求登录 接口")
            let res = await login(obj)
            // console.log(res);
            if (res.meta.status == 200) {
                // console.log("登录成功--- 改变state username token 返回成功状态 ")
                commit("SET_TOKEN", res.data.token)
                commit("SET_USERNAME", res.data.username)
                return Promise.resolve()

            } else {
                // console.log("登录失败-- 返回失败状态")
                return Promise.reject()
            }
        }
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.token = token;
            localStorage.setItem("houtaitoken", token)
        },
        SET_USERNAME(state, username) {
            state.username = username;
        },

        DEL_TOKEN(state) {
            localStorage.removeItem("houtaitoken");
        },
        DEL_USERNAME(state) {
            state.username = ""
        }


    },
}